<html>
<head><meta charset="utf-8"><title>avoiding UB due to races by discarding result? · t-lang/wg-unsafe-code-guidelines · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/index.html">t-lang/wg-unsafe-code-guidelines</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/avoiding.20UB.20due.20to.20races.20by.20discarding.20result.3F.html">avoiding UB due to races by discarding result?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="217343193"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/avoiding%20UB%20due%20to%20races%20by%20discarding%20result%3F/near/217343193" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Shnatsel <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/avoiding.20UB.20due.20to.20races.20by.20discarding.20result.3F.html#217343193">(Nov 19 2020 at 23:07)</a>:</h4>
<p>So apparently <code>crossbeam-deque</code> may perform racing reads, but then discard the result in case a race has occurred: <a href="https://github.com/crossbeam-rs/crossbeam/issues/589">https://github.com/crossbeam-rs/crossbeam/issues/589</a><br>
Is that legal? If not, how bad is it? This might be a good addition to the "UB used in practice" collection...</p>



<a name="217347014"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/avoiding%20UB%20due%20to%20races%20by%20discarding%20result%3F/near/217347014" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Connor Horman <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/avoiding.20UB.20due.20to.20races.20by.20discarding.20result.3F.html#217347014">(Nov 19 2020 at 23:57)</a>:</h4>
<p>I'd definitely put that under undefined, especially since it's volatile, as each volatile access is a side effect(?) on any affected scalar? Rust's memory model steal's Cs, but since rust doesn't have objects, and the C11 model is defined in terms of objects, it can actually be very difficult to reason about the memory model. <br>
To whether or not it could actively cause harm? <span aria-label="shrug" class="emoji emoji-1f937" role="img" title="shrug">:shrug:</span>, seems to me like it may not. Though as always, I feel obliged to point out that justifying UB is a great way to start justifying other UB, even if you are probably fine in this case at this time.</p>



<a name="217347504"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/avoiding%20UB%20due%20to%20races%20by%20discarding%20result%3F/near/217347504" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/avoiding.20UB.20due.20to.20races.20by.20discarding.20result.3F.html#217347504">(Nov 20 2020 at 00:02)</a>:</h4>
<p>Yeah, optimistic concurrency stuff like this is tricky. at one point i wrote a blog post about a thread-safe cell type whose loads could tear (<a href="https://shift.click/blog/tearcell/">https://shift.click/blog/tearcell/</a>). I meant to follow up with a post about example use cases but never got around to it — this sort of optimistic concurrency (also seqlocks, etc) are candidates of it. in theory if LLVM did a better job with optimizing that sort of thing, you could recommend it for that... in practice IIRC volatile read tends to generate much better code though, even than the more production-ready version of TearCell (which uses larger loads if possible).</p>



<a name="217368858"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/avoiding%20UB%20due%20to%20races%20by%20discarding%20result%3F/near/217368858" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/avoiding.20UB.20due.20to.20races.20by.20discarding.20result.3F.html#217368858">(Nov 20 2020 at 07:18)</a>:</h4>
<p>Hmm, how does it check that it raced?  I would assume that a racing read just gives poison, so you can perform it, you just can't use the value afterwards.</p>



<a name="217416980"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/avoiding%20UB%20due%20to%20races%20by%20discarding%20result%3F/near/217416980" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/avoiding.20UB.20due.20to.20races.20by.20discarding.20result.3F.html#217416980">(Nov 20 2020 at 15:42)</a>:</h4>
<p>A common way is to read a monotonically increasing write counter before and after. If the value is (even and) the same both times, then no race occurred.</p>



<a name="217453928"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/avoiding%20UB%20due%20to%20races%20by%20discarding%20result%3F/near/217453928" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/avoiding.20UB.20due.20to.20races.20by.20discarding.20result.3F.html#217453928">(Nov 20 2020 at 20:19)</a>:</h4>
<p><a href="http://llvm.org/docs/LangRef.html#memmodel">Looks like</a> the LLVM semantic is that a racing read produces <code>undef</code>.</p>
<p>So I don't know whether Rust adds any additional requirements atop it, but there's probably a version we could make allowed where reading it as <code>ManuallyUninit&lt;T&gt;</code> is fine and then only <code>assume_init</code>ed if some external thing says it didn't race.</p>
<p>Of course, it couldn't just be a normal <code>&amp;T</code> read of a non-<code>UnsafeCell</code> thing, since a read of that could definitely move earlier.</p>



<a name="217503131"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/avoiding%20UB%20due%20to%20races%20by%20discarding%20result%3F/near/217503131" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/avoiding.20UB.20due.20to.20races.20by.20discarding.20result.3F.html#217503131">(Nov 21 2020 at 14:05)</a>:</h4>
<p>Rust uses C/C++ semantics for concurrency</p>



<a name="217503136"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/avoiding%20UB%20due%20to%20races%20by%20discarding%20result%3F/near/217503136" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/avoiding.20UB.20due.20to.20races.20by.20discarding.20result.3F.html#217503136">(Nov 21 2020 at 14:05)</a>:</h4>
<p>this means racy reads are UB</p>



<a name="217503148"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/avoiding%20UB%20due%20to%20races%20by%20discarding%20result%3F/near/217503148" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/avoiding.20UB.20due.20to.20races.20by.20discarding.20result.3F.html#217503148">(Nov 21 2020 at 14:05)</a>:</h4>
<p>the two models are incompatible in that for each of the two, there are optimizations that work in one but not in the other (where "not work" = they are incorrect). LLVM actually used to have a bug where it did an optimization that is only correct under C++ concurrency semantics, which lead to miscompilations...</p>



<a name="217503222"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/avoiding%20UB%20due%20to%20races%20by%20discarding%20result%3F/near/217503222" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/avoiding.20UB.20due.20to.20races.20by.20discarding.20result.3F.html#217503222">(Nov 21 2020 at 14:07)</a>:</h4>
<p>The issue with the LLVM memory model is that it has seen hardly any formal scrutiny. the C/C++ model was broken in various ways until people started formalizing it properly and doing proofs about it (and the committee then adjusted the standard), so I'd expect the same to be true for the LLVM model.</p>



<a name="217503358"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/avoiding%20UB%20due%20to%20races%20by%20discarding%20result%3F/near/217503358" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/avoiding.20UB.20due.20to.20races.20by.20discarding.20result.3F.html#217503358">(Nov 21 2020 at 14:10)</a>:</h4>
<p><span class="user-mention silent" data-user-id="209168">Thom Chiovoloni</span> <a href="#narrow/stream/136281-t-lang.2Fwg-unsafe-code-guidelines/topic/avoiding.20UB.20due.20to.20races.20by.20discarding.20result.3F/near/217416980">said</a>:</p>
<blockquote>
<p>A common way is to read a monotonically increasing write counter before and after. If the value is (even and) the same both times, then no race occurred.</p>
</blockquote>
<p>due to what I said above, unfortunately such code is incorrect in Rust (just <a href="http://www.hpl.hp.com/techreports/2012/HPL-2012-68.pdf">like it is in C/C++</a>)</p>



<a name="217512764"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/avoiding%20UB%20due%20to%20races%20by%20discarding%20result%3F/near/217512764" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/avoiding.20UB.20due.20to.20races.20by.20discarding.20result.3F.html#217512764">(Nov 21 2020 at 18:28)</a>:</h4>
<p>Such code in C is running in the Linux kernel, on myriad systems.</p>



<a name="217512772"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/avoiding%20UB%20due%20to%20races%20by%20discarding%20result%3F/near/217512772" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/avoiding.20UB.20due.20to.20races.20by.20discarding.20result.3F.html#217512772">(Nov 21 2020 at 18:29)</a>:</h4>
<p>(deleted)</p>



<a name="217513106"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/avoiding%20UB%20due%20to%20races%20by%20discarding%20result%3F/near/217513106" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/avoiding.20UB.20due.20to.20races.20by.20discarding.20result.3F.html#217513106">(Nov 21 2020 at 18:38)</a>:</h4>
<p>Right, I wasn't disagreeing that it was UB, just giving a possible answer for  "how does it check that it raced?".</p>
<p>But yes, seqlocks are very common in the linux kernel.</p>



<a name="217513121"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/avoiding%20UB%20due%20to%20races%20by%20discarding%20result%3F/near/217513121" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/avoiding.20UB.20due.20to.20races.20by.20discarding.20result.3F.html#217513121">(Nov 21 2020 at 18:39)</a>:</h4>
<p>I am curious whether the kernel is doing something to make it not UB, or whether this is just a case of "in practice compilers handle it just fine".</p>



<a name="217513132"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/avoiding%20UB%20due%20to%20races%20by%20discarding%20result%3F/near/217513132" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/avoiding.20UB.20due.20to.20races.20by.20discarding.20result.3F.html#217513132">(Nov 21 2020 at 18:39)</a>:</h4>
<p>(Neither of those two would surprise me if it were the case.)</p>



<a name="217513343"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/avoiding%20UB%20due%20to%20races%20by%20discarding%20result%3F/near/217513343" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/avoiding.20UB.20due.20to.20races.20by.20discarding.20result.3F.html#217513343">(Nov 21 2020 at 18:44)</a>:</h4>
<p>I read through their seqlock code in the last couple months to answer that question for myself (and make sure a seqlock of my own (which used relaxed reads to avoid UB) had appropriate fencing) and it seemed that it was "in practice it's fine".</p>
<p>I mean, it seems hard for a compiler to miscompile in practice.</p>



<a name="217513412"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/avoiding%20UB%20due%20to%20races%20by%20discarding%20result%3F/near/217513412" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/avoiding.20UB.20due.20to.20races.20by.20discarding.20result.3F.html#217513412">(Nov 21 2020 at 18:46)</a>:</h4>
<p><a href="https://github.com/torvalds/linux/blob/672f8871261597894d46454b8fa1660d6c952af6/include/linux/seqlock.h">https://github.com/torvalds/linux/blob/672f8871261597894d46454b8fa1660d6c952af6/include/linux/seqlock.h</a> and <a href="https://github.com/torvalds/linux/blob/v3.13/arch/x86/vdso/vclock_gettime.c#L180-L187">https://github.com/torvalds/linux/blob/v3.13/arch/x86/vdso/vclock_gettime.c#L180-L187</a> are in my history for this if you're curious, although i now notice v3.13 is kinda old and maybe the code has changed since thhen (certainly it seems to have moved)</p>



<a name="217513428"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/avoiding%20UB%20due%20to%20races%20by%20discarding%20result%3F/near/217513428" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/avoiding.20UB.20due.20to.20races.20by.20discarding.20result.3F.html#217513428">(Nov 21 2020 at 18:46)</a>:</h4>
<p><span class="user-mention silent" data-user-id="239881">Josh Triplett</span> <a href="#narrow/stream/136281-t-lang.2Fwg-unsafe-code-guidelines/topic/avoiding.20UB.20due.20to.20races.20by.20discarding.20result.3F/near/217513121">said</a>:</p>
<blockquote>
<p>I am curious whether the kernel is doing something to make it not UB, or whether this is just a case of "in practice compilers handle it just fine".</p>
</blockquote>
<p>to my knowledge, the kernel doesn't use the C concurrency primitives, it does it's own hand-rolled thing</p>



<a name="217513450"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/avoiding%20UB%20due%20to%20races%20by%20discarding%20result%3F/near/217513450" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/avoiding.20UB.20due.20to.20races.20by.20discarding.20result.3F.html#217513450">(Nov 21 2020 at 18:47)</a>:</h4>
<p>also, this question (C++ vs LLVM concurrency) comes up again and again and I keep saying the same things^^ I wonder what would be a good place to write them down? (not that I have the time to actually do a writeup)</p>



<a name="217513517"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/avoiding%20UB%20due%20to%20races%20by%20discarding%20result%3F/near/217513517" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/avoiding.20UB.20due.20to.20races.20by.20discarding.20result.3F.html#217513517">(Nov 21 2020 at 18:49)</a>:</h4>
<p>The docs are very clear about it IMO, that we follow C++'s model.</p>



<a name="217513533"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/avoiding%20UB%20due%20to%20races%20by%20discarding%20result%3F/near/217513533" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/avoiding.20UB.20due.20to.20races.20by.20discarding.20result.3F.html#217513533">(Nov 21 2020 at 18:49)</a>:</h4>
<p><span class="user-mention silent" data-user-id="209168">Thom Chiovoloni</span> <a href="#narrow/stream/136281-t-lang.2Fwg-unsafe-code-guidelines/topic/avoiding.20UB.20due.20to.20races.20by.20discarding.20result.3F/near/217513343">said</a>:</p>
<blockquote>
<p>I read through their seqlock code in the last couple months to answer that question for myself (and make sure a seqlock of my own (which used relaxed reads to avoid UB) had appropriate fencing) and it seemed that it was "in practice it's fine".</p>
<p>I mean, it seems hard for a compiler to miscompile in practice.</p>
</blockquote>
<p>Section 2.3 of <a href="https://plv.mpi-sws.org/validc/paper.pdf">this paper</a> describes how code could be miscompiled when LLVM does optimizations both following the C++ model and following the LLVM model. That's not the same as miscompiling seqlock, clearly, but it could still be interesting to you.</p>



<a name="217513657"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/avoiding%20UB%20due%20to%20races%20by%20discarding%20result%3F/near/217513657" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/avoiding.20UB.20due.20to.20races.20by.20discarding.20result.3F.html#217513657">(Nov 21 2020 at 18:52)</a>:</h4>
<p>LLVM goes even further and makes write-write races not UB, but write <code>poison</code> instead.<br>
FWIW, I actually like this model, it seems nicer (for an IR, at least) than full UB on data races. But unfortunately, from what I understand, it does not fit very well the usual axiomatic "candidate execution" style of weak memory concurrency models. I anyway prefer operational models, but the main thrust of weak memory research is in the axiomatic style -- and the few people doing operational models have to compare with C++ as the gold standard, so little time is spent considering the LLVM approach. :/</p>



<a name="217513733"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/avoiding%20UB%20due%20to%20races%20by%20discarding%20result%3F/near/217513733" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/avoiding.20UB.20due.20to.20races.20by.20discarding.20result.3F.html#217513733">(Nov 21 2020 at 18:54)</a>:</h4>
<p>Hm, interesting, I'll give it a read.</p>
<p>And I think the misconception is the same misconception here are the same as the misconceptions in another thread about volitile reads being a way to dodge UB. (Since crossbeam reads this with a volatile read).</p>
<p>Using something like <code>TearCell</code> (linked before) would avoid this, and an actually-good implementation of it would probably even avoid performance issues, but I don't think you can define an efficient TearCell for non <code>bytemuck::Pod</code> types due issues with reading padding bytes causing a wider read to be entirely undef (or poison? I don't know), as opposed to only the bytes in question being poison.</p>
<p>LLVM's <code>atomic_element_unordered_copy_memory</code> stuff might help here but I suspect it's undesirable in Rust (unordered isn't part of the C memory model) and even then currently it is not efficient at all.</p>



<a name="217513751"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/avoiding%20UB%20due%20to%20races%20by%20discarding%20result%3F/near/217513751" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/avoiding.20UB.20due.20to.20races.20by.20discarding.20result.3F.html#217513751">(Nov 21 2020 at 18:55)</a>:</h4>
<blockquote>
<p>(Since crossbeam reads this with a volatile read).</p>
</blockquote>
<p>the crossbeam authors are aware that <code>volatile</code> doesn't fix the UB. they did this to reduce the chance of miscompilation.</p>



<a name="217513838"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/avoiding%20UB%20due%20to%20races%20by%20discarding%20result%3F/near/217513838" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/avoiding.20UB.20due.20to.20races.20by.20discarding.20result.3F.html#217513838">(Nov 21 2020 at 18:57)</a>:</h4>
<p>I see. You see the same issue in the seqlock crate: <a href="https://github.com/Amanieu/seqlock/blob/master/src/lib.rs#L147">https://github.com/Amanieu/seqlock/blob/master/src/lib.rs#L147</a> . Admittedly, the only use of that code I know of is in the crossbeam benchmarks, but it's plausible there are others.</p>



<a name="217514715"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/avoiding%20UB%20due%20to%20races%20by%20discarding%20result%3F/near/217514715" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/avoiding.20UB.20due.20to.20races.20by.20discarding.20result.3F.html#217514715">(Nov 21 2020 at 19:17)</a>:</h4>
<p>My recommendation for such cases would be to acknowledge in the comment that this is not standard-compliant, and to remain on the look (via testing etc) for actual issues arising from this</p>



<a name="217514912"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/avoiding%20UB%20due%20to%20races%20by%20discarding%20result%3F/near/217514912" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/avoiding.20UB.20due.20to.20races.20by.20discarding.20result.3F.html#217514912">(Nov 21 2020 at 19:22)</a>:</h4>
<p>What would the missed optimization consequences be for moving to the "candidate execution" model for racing memory operations, compatible with algorithms that assume a read racing with a write will obtain either the old or new value, and two writes racing will happen in one or the other order?</p>



<a name="217514924"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/avoiding%20UB%20due%20to%20races%20by%20discarding%20result%3F/near/217514924" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/avoiding.20UB.20due.20to.20races.20by.20discarding.20result.3F.html#217514924">(Nov 21 2020 at 19:23)</a>:</h4>
<p>What optimization opportunities does LLVM gain with its model?</p>



<a name="217515275"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/avoiding%20UB%20due%20to%20races%20by%20discarding%20result%3F/near/217515275" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/avoiding.20UB.20due.20to.20races.20by.20discarding.20result.3F.html#217515275">(Nov 21 2020 at 19:32)</a>:</h4>
<p><span class="user-mention silent" data-user-id="239881">Josh Triplett</span> <a href="#narrow/stream/136281-t-lang.2Fwg-unsafe-code-guidelines/topic/avoiding.20UB.20due.20to.20races.20by.20discarding.20result.3F/near/217514912">said</a>:</p>
<blockquote>
<p>What would the missed optimization consequences be for moving to the "candidate execution" model for racing memory operations, compatible with algorithms that assume a read racing with a write will obtain either the old or new value, and two writes racing will happen in one or the other order?</p>
</blockquote>
<p>I'm the wrong person to ask this... I have explicitly avoided becoming a weak memory expert.^^ but there definitely are optimizations that rely on read-write races not just returning the old or the new value.</p>



<a name="217515298"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/avoiding%20UB%20due%20to%20races%20by%20discarding%20result%3F/near/217515298" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/avoiding.20UB.20due.20to.20races.20by.20discarding.20result.3F.html#217515298">(Nov 21 2020 at 19:33)</a>:</h4>
<p><span class="user-mention silent" data-user-id="239881">Josh Triplett</span> <a href="#narrow/stream/136281-t-lang.2Fwg-unsafe-code-guidelines/topic/avoiding.20UB.20due.20to.20races.20by.20discarding.20result.3F/near/217514924">said</a>:</p>
<blockquote>
<p>What optimization opportunities does LLVM gain with its model?</p>
</blockquote>
<p>see <a href="https://plv.mpi-sws.org/validc/paper.pdf">section 2.3 of this paper</a>, which discusses two optimizations: one allowed in LLVM and not allowed in GCC (as GCC uses the C++ model all the way though), and one vice versa.</p>



<a name="217515381"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/avoiding%20UB%20due%20to%20races%20by%20discarding%20result%3F/near/217515381" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/avoiding.20UB.20due.20to.20races.20by.20discarding.20result.3F.html#217515381">(Nov 21 2020 at 19:35)</a>:</h4>
<p>basically, LLVM can introduce speculative reads, while GCC cannot</p>



<a name="221369080"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/avoiding%20UB%20due%20to%20races%20by%20discarding%20result%3F/near/221369080" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thales Fragoso <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/avoiding.20UB.20due.20to.20races.20by.20discarding.20result.3F.html#221369080">(Jan 01 2021 at 21:11)</a>:</h4>
<blockquote>
<p>due to what I said above, unfortunately such code is incorrect in Rust (just <a href="http://www.hpl.hp.com/techreports/2012/HPL-2012-68.pdf">like it is in C/C++</a>)</p>
</blockquote>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span>  is figure 2 on this paper correct ? I thought atomics would only sync between themselves, they even talk about this in section 4</p>



<a name="221369475"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/avoiding%20UB%20due%20to%20races%20by%20discarding%20result%3F/near/221369475" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/avoiding.20UB.20due.20to.20races.20by.20discarding.20result.3F.html#221369475">(Jan 01 2021 at 21:21)</a>:</h4>
<p>The default synchronization of <code>atomic&lt;T&gt;</code> in C++ is <code>SeqCst</code>, so they can be used to synchronize other variables</p>



<a name="221369490"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/avoiding%20UB%20due%20to%20races%20by%20discarding%20result%3F/near/221369490" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thales Fragoso <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/avoiding.20UB.20due.20to.20races.20by.20discarding.20result.3F.html#221369490">(Jan 01 2021 at 21:21)</a>:</h4>
<p>I thought that wasn't the case for any memory ordering, even SeqCst</p>



<a name="221369539"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/avoiding%20UB%20due%20to%20races%20by%20discarding%20result%3F/near/221369539" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thales Fragoso <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/avoiding.20UB.20due.20to.20races.20by.20discarding.20result.3F.html#221369539">(Jan 01 2021 at 21:22)</a>:</h4>
<p>See Section 4 of the same paper</p>



<a name="221369561"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/avoiding%20UB%20due%20to%20races%20by%20discarding%20result%3F/near/221369561" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/avoiding.20UB.20due.20to.20races.20by.20discarding.20result.3F.html#221369561">(Jan 01 2021 at 21:23)</a>:</h4>
<p>With sequential consistency as traditionally conceived, you know that the write to <code>x</code> happens before <code>y = 1</code>, which happens before the while loop fails, which happens before the read in the assert</p>



<a name="221369571"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/avoiding%20UB%20due%20to%20races%20by%20discarding%20result%3F/near/221369571" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thales Fragoso <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/avoiding.20UB.20due.20to.20races.20by.20discarding.20result.3F.html#221369571">(Jan 01 2021 at 21:23)</a>:</h4>
<p>But what prevents the load being moved to after the store with SeqCst ?</p>



<a name="221369619"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/avoiding%20UB%20due%20to%20races%20by%20discarding%20result%3F/near/221369619" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/avoiding.20UB.20due.20to.20races.20by.20discarding.20result.3F.html#221369619">(Jan 01 2021 at 21:24)</a>:</h4>
<p>The happens before relations I just described</p>



<a name="221369638"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/avoiding%20UB%20due%20to%20races%20by%20discarding%20result%3F/near/221369638" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thales Fragoso <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/avoiding.20UB.20due.20to.20races.20by.20discarding.20result.3F.html#221369638">(Jan 01 2021 at 21:25)</a>:</h4>
<p>Like I said before, I thought that for atomics that would only guarantee ordering between the same variable</p>



<a name="221369651"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/avoiding%20UB%20due%20to%20races%20by%20discarding%20result%3F/near/221369651" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thales Fragoso <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/avoiding.20UB.20due.20to.20races.20by.20discarding.20result.3F.html#221369651">(Jan 01 2021 at 21:25)</a>:</h4>
<p>And in my mind that was the difference between ordering in atomics and fences</p>



<a name="221369652"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/avoiding%20UB%20due%20to%20races%20by%20discarding%20result%3F/near/221369652" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/avoiding.20UB.20due.20to.20races.20by.20discarding.20result.3F.html#221369652">(Jan 01 2021 at 21:25)</a>:</h4>
<p>SeqCst talks about a global order though, that is consistent with happens-before</p>



<a name="221369659"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/avoiding%20UB%20due%20to%20races%20by%20discarding%20result%3F/near/221369659" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thales Fragoso <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/avoiding.20UB.20due.20to.20races.20by.20discarding.20result.3F.html#221369659">(Jan 01 2021 at 21:25)</a>:</h4>
<p>So, is section 4 of the same paper wrong ?</p>



<a name="221369842"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/avoiding%20UB%20due%20to%20races%20by%20discarding%20result%3F/near/221369842" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/avoiding.20UB.20due.20to.20races.20by.20discarding.20result.3F.html#221369842">(Jan 01 2021 at 21:29)</a>:</h4>
<p>In</p>
<div class="codehilite" data-code-language="C++"><pre><span></span><code><span class="kt">int</span> <span class="n">r2</span> <span class="o">=</span> <span class="n">data2</span><span class="p">;</span>
<span class="n">seq1</span> <span class="o">=</span> <span class="n">seq</span><span class="p">;</span>
</code></pre></div>
<p>(where I guess the <code>seq</code> variables are atomic), the load happens-before the other load but that doesn't mean anything in particular in this example because nothing reads <code>r2</code></p>



<a name="221369922"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/avoiding%20UB%20due%20to%20races%20by%20discarding%20result%3F/near/221369922" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/avoiding.20UB.20due.20to.20races.20by.20discarding.20result.3F.html#221369922">(Jan 01 2021 at 21:31)</a>:</h4>
<p>I'm not sure what specifically you are pointing to that you think is wrong</p>



<a name="221369940"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/avoiding%20UB%20due%20to%20races%20by%20discarding%20result%3F/near/221369940" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thales Fragoso <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/avoiding.20UB.20due.20to.20races.20by.20discarding.20result.3F.html#221369940">(Jan 01 2021 at 21:31)</a>:</h4>
<blockquote>
<p>The second statement performs a “sequentially consistent” atomic load from seq. But that is not required to<br>
prevent the processor or compiler from reordering the two<br>
loads.</p>
</blockquote>



<a name="221369988"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/avoiding%20UB%20due%20to%20races%20by%20discarding%20result%3F/near/221369988" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thales Fragoso <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/avoiding.20UB.20due.20to.20races.20by.20discarding.20result.3F.html#221369988">(Jan 01 2021 at 21:32)</a>:</h4>
<p>Hmm, I think I know what I got wrong</p>



<a name="221370020"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/avoiding%20UB%20due%20to%20races%20by%20discarding%20result%3F/near/221370020" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/avoiding.20UB.20due.20to.20races.20by.20discarding.20result.3F.html#221370020">(Jan 01 2021 at 21:33)</a>:</h4>
<p>that's true. I said that it "happens-before" but that doesn't mean that the processor or compiler actually has to put them in order unless this hb relation is used to deduce that a read sees a write</p>



<a name="221370074"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/avoiding%20UB%20due%20to%20races%20by%20discarding%20result%3F/near/221370074" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/avoiding.20UB.20due.20to.20races.20by.20discarding.20result.3F.html#221370074">(Jan 01 2021 at 21:34)</a>:</h4>
<p>because that's the only "observable content" of the happens before relation</p>



<a name="221370099"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/avoiding%20UB%20due%20to%20races%20by%20discarding%20result%3F/near/221370099" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thales Fragoso <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/avoiding.20UB.20due.20to.20races.20by.20discarding.20result.3F.html#221370099">(Jan 01 2021 at 21:35)</a>:</h4>
<p>Hmm, I read the docs again and I think I had some misconceptions about the ordering on atomics</p>



<a name="221370108"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/avoiding%20UB%20due%20to%20races%20by%20discarding%20result%3F/near/221370108" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thales Fragoso <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/avoiding.20UB.20due.20to.20races.20by.20discarding.20result.3F.html#221370108">(Jan 01 2021 at 21:35)</a>:</h4>
<p>They are stronger than I thought</p>



<a name="221370162"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/avoiding%20UB%20due%20to%20races%20by%20discarding%20result%3F/near/221370162" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/avoiding.20UB.20due.20to.20races.20by.20discarding.20result.3F.html#221370162">(Jan 01 2021 at 21:36)</a>:</h4>
<p>in particular, <code>Release</code> and <code>Acquire</code> orderings are specifically intended to be able to synchronize variables other than the atomic itself</p>



<a name="221370173"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/avoiding%20UB%20due%20to%20races%20by%20discarding%20result%3F/near/221370173" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/avoiding.20UB.20due.20to.20races.20by.20discarding.20result.3F.html#221370173">(Jan 01 2021 at 21:37)</a>:</h4>
<p>and <code>SeqCst</code> is viewed as implying both of these (although I think it is disputed whether this is actually true in the C++11 memory model)</p>



<a name="221370233"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/avoiding%20UB%20due%20to%20races%20by%20discarding%20result%3F/near/221370233" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thales Fragoso <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/avoiding.20UB.20due.20to.20races.20by.20discarding.20result.3F.html#221370233">(Jan 01 2021 at 21:38)</a>:</h4>
<p>Yeah, I get it now, I just got a bit confused with some wording I guess</p>



<a name="221398548"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/avoiding%20UB%20due%20to%20races%20by%20discarding%20result%3F/near/221398548" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/avoiding.20UB.20due.20to.20races.20by.20discarding.20result.3F.html#221398548">(Jan 02 2021 at 12:44)</a>:</h4>
<p><span class="user-mention silent" data-user-id="271719">Mario Carneiro</span> <a href="#narrow/stream/136281-t-lang.2Fwg-unsafe-code-guidelines/topic/avoiding.20UB.20due.20to.20races.20by.20discarding.20result.3F/near/221370173">said</a>:</p>
<blockquote>
<p>and <code>SeqCst</code> is viewed as implying both of these (although I think it is disputed whether this is actually true in the C++11 memory model)</p>
</blockquote>
<p>no, that's not disputed. there were bugs in the model but I think the bugs had a different effect than this. and anyway they are fixed in c++2x</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>